package com.imooc.web.controller;

import com.imooc.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zghgchao 2018/4/15 13:22
 */
@ControllerAdvice//只负责处理其他控制器抛出来的异常，本身并不处理HTTP请求
public class ControllerExceptionHandler {

    @ExceptionHandler(UserNotExistException.class)//处理UserNotExistException异常
    @ResponseBody//返回json字符串
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)//返回的状态码：500
    public Map<String,Object> handlerUserNotExistException(UserNotExistException e){//通过参数拿到异常信息
        Map<String,Object> result = new HashMap<>();
        result.put("id",e.getId());
        result.put("message",e.getMessage());
        return result;
    }
}
